

import { defineStore } from 'pinia';
import { ref } from 'vue';

interface User {
  username: string;
  email: string;
  // 根据需要扩展更多字段
}

export const useAuthStore = defineStore('auth', () => {
  const token = ref<string | null>(localStorage.getItem('auth_token') || null);
  const user = ref<User | null>(null);

  // 设置 token
  const setToken = (newToken: string) => {
    token.value = newToken;
    localStorage.setItem('auth_token', newToken);
  };

  // 设置用户信息
  const setUser = (userInfo: User) => {
    user.value = userInfo;
  };

  // 清除登录状态
  const logout = () => {
    token.value = null;
    user.value = null;
    localStorage.removeItem('auth_token');
  };

  return { token, user, setToken, setUser, logout };
});